package com.broad.commerce.code.services.business;

import com.broad.commerce.code.base.AbstractJavaService;
import com.broad.commerce.code.modules.BaseModule;
import com.broad.commerce.code.modules.business.ControllerModule;
import com.broad.commerce.common.utils.core.StringUtils;
import com.broad.commerce.web.exception.ServiceException;
import org.springframework.stereotype.Service;

/**
 * @author stephen
 * @version V1.0
 * @date 2022-02-08 18:54
 * @description: ControllerModuleService
 **/
@SuppressWarnings("DuplicatedCode")
@Service
public class ControllerModuleService extends AbstractJavaService {

	/**
	 * Create entity code string.
	 * @param baseModule the base module
	 * @return the string
	 */
	@Override
	public String createClassCode(BaseModule baseModule) {
		if (!(baseModule instanceof ControllerModule)) {
			throw new ServiceException("传入参数类型错误！应为 ControllerModule");
		}
		return super.createClassCode(baseModule);
	}

	/**
	 * Create import code.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	@Override
	protected void createImportCode(StringBuilder sb, BaseModule baseModule) {
		ControllerModule controllerModule = (ControllerModule) baseModule;
		//@off
		sb.append("import ").append(controllerModule.getInterfaceFullClassName()).append(";\n")
		  .append("import ").append(controllerModule.getEntityFullClassName()).append(";\n")
		  .append("import ").append(controllerModule.getModelFullClassName()).append(";\n")
		  .append("import com.broad.commerce.common.constants.MediaTypes;").append("\n")
		  .append("import org.springframework.beans.factory.annotation.Autowired;").append("\n")
		  .append("import org.springframework.web.bind.annotation.RequestBody;").append("\n")
		  .append("import org.springframework.web.bind.annotation.RequestMapping;").append("\n")
		  .append("import org.springframework.web.bind.annotation.RequestMethod;").append("\n")
		  .append("import org.springframework.web.bind.annotation.RequestParam;").append("\n")
		  .append("import org.springframework.web.bind.annotation.RestController;").append("\n")
		  .append("\n")
		  .append("import javax.validation.Valid;").append("\n")
		  .append("import java.util.List;").append("\n")
		  .append("\n");//@on
	}

	/**
	 * Create annotation code.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	@Override
	protected void createAnnotationCode(StringBuilder sb, BaseModule baseModule) {
		ControllerModule controllerModule = (ControllerModule) baseModule;
		//@off
		sb.append("@RestController").append("\n")
		  .append("@RequestMapping(value = \"").append(controllerModule.getMappingRootPath()).append("\")").append("\n");//@on
	}

	/**
	 * Create class start tag.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	@Override
	protected void createClassStartTag(StringBuilder sb, BaseModule baseModule) {
		ControllerModule controllerModule = (ControllerModule) baseModule;
		sb.append("public class ").append(controllerModule.getClassName()).append(" {\n");
	}

	/**
	 * Create class content.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	@Override
	protected void createClassContent(StringBuilder sb, BaseModule baseModule) {
		ControllerModule controllerModule = (ControllerModule) baseModule;
		this.createClassInjectionContent(sb, controllerModule);
		String entityName = controllerModule.getEntityClassName();
		String modelName = controllerModule.getModelClassName();
		String entityInstance = StringUtils.lowerFirst(entityName);
		String modelInstance = StringUtils.lowerFirst(modelName);
		String serviceName = StringUtils.lowerFirst(controllerModule.getServiceClassName());
		//@off 查询所有
		sb.append("\t").append("@RequestMapping(value = \"/findAll\", method = RequestMethod.GET, produces = MediaTypes.APPLICATION_JSON_UTF8)\n")
		  .append("\t").append("public List<").append(entityName).append("> findAll() {\n")
		  .append("\t\t").append("return this.").append(serviceName).append(".selectAll();\n")
		  .append("\t}\n\n");
		//查询单个数据
		sb.append("\t").append("@RequestMapping(value = \"/findOne\", method = RequestMethod.GET, produces = MediaTypes.APPLICATION_JSON_UTF8)\n")
		  .append("\t").append("public ").append(entityName).append(" findOne(@RequestParam Long id) {\n")
		  .append("\t\t").append("return this.").append(serviceName).append(".findOne(id);\n")
		  .append("\t}\n\n");
		//插入单个数据
		sb.append("\t").append("@RequestMapping(value = \"/save\", method = RequestMethod.POST, produces = MediaTypes.APPLICATION_JSON_UTF8)\n")
		  .append("\t").append("public void save(@RequestBody @Valid ").append(modelName).append(" ").append(modelInstance).append(") {\n")
		  .append("\t\t").append("this.").append(serviceName).append(".save(").append(modelInstance).append(");\n")
		  .append("\t}\n\n");
		//批量插入数据
		sb.append("\t").append("@RequestMapping(value = \"/saveAll\", method = RequestMethod.POST, produces = MediaTypes.APPLICATION_JSON_UTF8)\n")
		  .append("\t").append("public void saveAll(@RequestBody @Valid List<").append(modelName).append("> ").append(modelInstance).append("S) {\n")
		  .append("\t\t").append("this.").append(serviceName).append(".saveAll(").append(modelInstance).append("S);\n")
		  .append("\t}\n\n");
		//更新单个数据
		sb.append("\t").append("@RequestMapping(value = \"/update\", method = RequestMethod.POST, produces = MediaTypes.APPLICATION_JSON_UTF8)\n")
		  .append("\t").append("public void update(@RequestBody @Valid ").append(modelName).append(" ").append(modelInstance).append(") {\n")
		  .append("\t\t").append("this.").append(serviceName).append(".save(").append(modelInstance).append(");\n")
		  .append("\t}\n\n");
		//删除单个数据
		sb.append("\t").append("@RequestMapping(value = \"/delete\", method = RequestMethod.GET, produces = MediaTypes.APPLICATION_JSON_UTF8)\n")
		  .append("\t").append("public void delete(@RequestParam Long id) {\n")
		  .append("\t\t").append("this.").append(serviceName).append(".delete(id);\n")
		  .append("\t}\n");//@on
	}

	/**
	 * Create class injection content.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	private void createClassInjectionContent(StringBuilder sb, ControllerModule controllerModule) {
		//@off
		sb.append("\t").append("@Autowired").append("\n")
		  .append("\t").append("private ").append(controllerModule.getInterfaceClassName()).append(" ")
		  .append(StringUtils.lowerFirst(controllerModule.getServiceClassName())).append(";\n")
		  .append("\n");//@on
	}
}
